﻿Imports MySql.Data.MySqlClient
Public Class FrmPoDetail
    Dim DA As New MySqlDataAdapter
    Dim BDS As New BindingSource
    Dim CMDB As New MySqlCommandBuilder
    Dim Strsql As String
    Dim Mysql As New ClsSQLhelper
    Dim DSWHIS As New DSWHIS
    Public NewEntry_State As Boolean
    Dim MyWhis As New cls_whisframework

    'Dim On_Load As Boolean = True
    'Dim lOldValue As Object
    'Dim lNewValue As Object
#Region "Function"
    Sub Load_Budget()
        Try
            With Me.CboYearBudget
                Dim DTBudget As New DataTable
                Strsql = "select bdg_year from yearbudget order by bdg_year desc "
                DTBudget = Mysql.GetMYSQLDataTable(Strsql, "yearbudget")
                .DataSource = DTBudget
                .DisplayMember = "bdg_year"
                .ValueMember = "bdg_year"
                'DTBudget.Dispose()
                .SelectedValue = MyWhis.Get_CurrentYearbudget(ThaiFormat:=True)
            End With
        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "Load_Budget"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try
        
    End Sub
    Sub Load_Site()
        Try
            With Me.CboSite
                Dim DTSite As New DataTable
                Strsql = "Select siteid,site from site order by site"
                DTSite = Mysql.GetMYSQLDataTable(Strsql, "site")
                .DataSource = DTSite
                .DisplayMember = "site"
                .ValueMember = "siteid"
                'DTSite.Dispose()
                .SelectedValue = My.Settings.CurrentSite
            End With
        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "Load_Site"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try

    End Sub
    Sub Load_CboDepartment()
        Try
            Dim DTdepartment As New DataTable
            Strsql = "Select depcode,depname from departments order by depname"
            Me.EventLog1.WriteEntry(Me.Name & " : " & Strsql, EventLogEntryType.Information, 1)
            DTdepartment = Mysql.GetMYSQLDataTable(Strsql, "departments")
            With cboDepartment
                .DataSource = DTdepartment
                .DisplayMember = "depname"
                .ValueMember = "depcode"
                .SelectionStart = 0
            End With
            'DTdepartment.Dispose()
        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "Load_CboDepartment"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try

    End Sub
 
    Sub Load_CboOrderStatus()
        Try
            With CboOrderStatus
                Dim DTOrderStatus As New DataTable
                Strsql = "Select orderstatusid,orderstatus from orderstatus"
                DTOrderStatus = Mysql.GetMYSQLDataTable(Strsql, "orderstatus")
                .DataSource = DTOrderStatus
                .DisplayMember = "orderstatus"
                .ValueMember = "orderstatusid"
                .SelectionStart = 0
            End With
        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "Load_CboOrderStatus"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try

    End Sub
    Sub Load_Data()

        Try
            Dim DS_PRdetail As New DataSet
            Dim DS_PROrder As New DataSet
            Dim DT_Item As New DataTable

            Dim Col_Products As New DataGridViewComboBoxColumn
            Dim Col_Uom As New DataGridViewTextBoxColumn
            Dim Col_OrderQty As New DataGridViewTextBoxColumn
            Dim Col_OnHandQty As New DataGridViewTextBoxColumn
            Dim Col_Drawqty As New DataGridViewTextBoxColumn
            Dim Col_Price As New DataGridViewTextBoxColumn
            Dim Col_CurrPrice As New DataGridViewTextBoxColumn
            Dim Col_Note As New DataGridViewTextBoxColumn
            Dim Col_Totalprice As New DataGridViewTextBoxColumn
            Dim Col_OrderNumber As New DataGridViewTextBoxColumn
            Dim MyWhis As New cls_whisframework

            Me.TxtOrderedBy.Text = MyWhis.Get_CurrentUserfullname


            Dim i As Integer = 0

            With Col_OrderNumber
                .Visible = False
                .DataPropertyName = "ordernumber"
            End With

            Strsql = "select item.itemnumber,concat(item.tradename,' ',item.itemname) as itemname from siteitem left join item on siteitem.itemnumber = item.itemnumber left join itemcontainunit on  siteitem.itemnumber = itemcontainunit.itemid where siteitem.site = " & Me.CboSite.SelectedValue & " group by  item.itemnumber   order by itemname"
            DT_Item = Mysql.GetMYSQLDataTable(Strsql, "item")
            With Col_Products
                .DataSource = DT_Item
                .DisplayMember = "itemname"
                .ValueMember = "itemnumber"
                .HeaderText = "ชื่อสินค้า"
                .Width = 300
                .DataPropertyName = "itemnumber"
            End With

            With Col_OrderQty
                .HeaderText = "จำนวน"
                .Width = 60
                .DefaultCellStyle.NullValue = "1"
                .DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
                .DataPropertyName = "orderedqty"
            End With

            With Col_Uom
                .HeaderText = "หน่วยบรรจุ"
                .Width = 100
                .DataPropertyName = "containunit"
            End With

            With Col_OnHandQty
                .HeaderText = "ในสต็อก"
                .Width = 80
                .DefaultCellStyle.Format = "N0"
                .DefaultCellStyle.NullValue = "0"
                .DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
                .DataPropertyName = "onhandqty"
                .ReadOnly = True
            End With

            With Col_Price
                .HeaderText = "ราคากลาง"
                .Width = 80
                .DefaultCellStyle.Format = "N0"
                .DefaultCellStyle.NullValue = "0"
                .DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
                .DataPropertyName = "priceperitem"
                .ReadOnly = True
            End With

            With Col_CurrPrice
                .HeaderText = "ราคาครั้งนี้"
                .Width = 80
                .DefaultCellStyle.Format = "N0"
                .DefaultCellStyle.NullValue = "0"
                .DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
                .DataPropertyName = "currentprice"
            End With

            With Col_Totalprice
                .HeaderText = "รวมมูลค่า"
                .Width = 80
                .DefaultCellStyle.Format = "N0"
                .DefaultCellStyle.NullValue = "0"
                .DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
                .DataPropertyName = "totalprice"
                .ReadOnly = True
            End With


            With Col_Note
                .HeaderText = "หมายเหตุ"
                .Width = 100
                .DataPropertyName = "note"
            End With



            With GridOrderdetail

                .AutoGenerateColumns = False
                .Columns.Add(Col_Products)
                .Columns.Add(Col_OrderQty)
                .Columns.Add(Col_Uom)
                .Columns.Add(Col_OnHandQty)
                .Columns.Add(Col_Price)
                .Columns.Add(Col_CurrPrice)
                .Columns.Add(Col_Totalprice)
                .Columns.Add(Col_Note)
                .Columns.Add(Col_OrderNumber)

                Select Case NewEntry_State
                    Case True

                    Case False
                        Dim DTPROrder As New DataTable
                        Strsql = "Select ordernumber,orderdate,yearbudget,site,department,orderedby,producttype,reason from poorder where ordernumber = " & Me.TxtOrderNumber.Text
                        Me.EventLog1.WriteEntry(Me.Name & " : " & Strsql, EventLogEntryType.Information, 1)
                        DTPROrder = Mysql.GetMYSQLDataTable(Strsql, "prorder")
                        ' Me.TxtOrderNumber.Text = DTPROrder.Rows(0).Item("ordernumber").ToString
                        Me.TxtOrderedBy.Text = DTPROrder.Rows(0).Item("orderedby").ToString
                        Me.TxtProductType.Text = DTPROrder.Rows(0).Item("producttype").ToString
                        Me.TxtReason.Text = DTPROrder.Rows(0).Item("reason").ToString
                        Me.DTPOrderedDate.Value = DTPROrder.Rows(0).Item("orderdate")
                        Me.CboSite.SelectedValue = DTPROrder.Rows(0).Item("site")
                        Me.cboDepartment.SelectedValue = DTPROrder.Rows(0).Item("department")
                        Me.CboYearBudget.SelectedValue = DTPROrder.Rows(0).Item("yearbudget")


                        Strsql = "Select * from podetail where ordernumber = '" & Me.TxtOrderNumber.Text & "'"
                        Me.EventLog1.WriteEntry(Me.Name & " : " & Strsql, EventLogEntryType.Information, 1)
                        DA = New MySqlDataAdapter(Strsql, ClsSQLhelper.MySQLConnectionStr)
                        DA.Fill(DSWHIS, "podetail")
                        '  BDS = New BindingSource(DSWHIS.Tables("prdetail"), "prdetail")
                        CMDB = New MySqlCommandBuilder(DA)
                        .DataSource = DSWHIS.Tables("podetail")
                End Select





            End With
        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "Load_Data"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try


    End Sub
    Function Validate_Data() As Boolean
        Try
            Validate_Data = False
            Select Case NewEntry_State
                Case True
                    Strsql = "Select Count(*) as rowcount from poorder where ordernumber = '" & Me.TxtOrderNumber.Text & "' and site = " & Me.CboSite.SelectedValue
                    If Mysql.MySQLExecuteScalar(Strsql) > 0 Then
                        MsgBox("Order นี้ได้ถูกใช้งานแล้ว ไม่สามารถใช้งานได้อีก", MsgBoxStyle.Critical)
                        Return Validate_Data
                        Exit Function
                    End If

                Case False

            End Select


            Validate_Data = True
            Return Validate_Data
        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "Validate_Data"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try

    End Function
    'Sub Load_Data()
    '    Dim DT_PRdetail As New DataTable
    '    Dim DS_PROrder As New DataSet
    '    Dim DT_Item As New DataTable

    '    ' Me.BtnUnlock.Left = Me.BtnLock.Left
    '    ' Me.BtnUnlock.Top = Me.BtnLock.Top


    '    EventLog1.Source = "WHIS"
    '    EventLog1.Log = "WHIS"




    '    '  frmPROrder.Hide()



    '    'Select Case frmPROrder.CboOrderStatus.SelectedValue
    '    '    Case Is <> 1
    '    '        'BtnLock.Enabled = False
    '    '        Me.GridOrderdetail.Enabled = False
    '    '        Me.BtnDelete.Enabled = False
    '    '        Me.BtnSave.Enabled = False
    '    '        frmPROrder.Cbo_site.Enabled = False
    '    '        Me.cboDepartment.Enabled = False
    '    '        frmPROrder.CboOrderStatus.Enabled = False
    '    '        Me.DTPOrderedDate.Enabled = False
    '    '        Me.BtnLock.Visible = False
    '    '        ' Me.BtnUnlock.Visible = True
    '    '    Case Else
    '    '        Me.GridOrderdetail.Enabled = True
    '    '        Me.BtnDelete.Enabled = True
    '    '        Me.BtnSave.Enabled = True
    '    '        frmPROrder.Cbo_site.Enabled = True
    '    '        Me.cboDepartment.Enabled = True
    '    '        frmPROrder.CboOrderStatus.Enabled = True
    '    '        Me.DTPOrderedDate.Enabled = True
    '    '        Me.BtnLock.Visible = True
    '    '        'Me.BtnUnlock.Visible = False
    '    'End Select


    '    'Me.TxtOrderedBy.Text = My.Settings.Currentuser

    '    With GridOrderdetail

    '        Dim Col_Products As New DataGridViewComboBoxColumn
    '        Dim Col_Uom As New DataGridViewTextBoxColumn
    '        Dim Col_OrderQty As New DataGridViewTextBoxColumn
    '        Dim Col_OnHandQty As New DataGridViewTextBoxColumn
    '        Dim Col_Drawqty As New DataGridViewTextBoxColumn
    '        Dim Col_Price As New DataGridViewTextBoxColumn
    '        Dim Col_Note As New DataGridViewTextBoxColumn
    '        Dim Col_Totalprice As New DataGridViewTextBoxColumn
    '        Dim Col_OrderNumber As New DataGridViewTextBoxColumn

    '        Dim i As Integer = 0

    '        .RowHeadersVisible = True
    '        .AllowDrop = False
    '        .AllowUserToAddRows = True
    '        .AllowUserToDeleteRows = True
    '        .AllowUserToOrderColumns = False
    '        .AllowUserToResizeColumns = True
    '        .AllowUserToResizeRows = False
    '        .MultiSelect = False
    '        .ReadOnly = False


    '        Strsql = "SELECT item.itemnumber,item.itemname from item right join itemcontainunit on item.itemnumber= itemcontainunit.itemid group by  item.itemnumber   order by itemname"
    '        DT_Item = Mysql.GetMYSQLDataTable(Strsql, "item")
    '        Me.EventLog1.WriteEntry(Me.Name & " : " & Strsql, EventLogEntryType.Information, 1)
    '        DT_Item = Mysql.GetMYSQLDataTable(Strsql, "item")


    '        With Col_OrderNumber
    '            .Visible = False
    '            .DataPropertyName = "ordernumber"
    '        End With

    '        Strsql = "SELECT item.itemnumber,item.itemname from item right join itemcontainunit on item.itemnumber= itemcontainunit.itemid group by  item.itemnumber   order by itemname"
    '        DT_Item = Mysql.GetMYSQLDataTable(Strsql, "item")
    '        With Col_Products
    '            .DataSource = DT_Item
    '            .DisplayMember = "itemname"
    '            .ValueMember = "itemnumber"
    '            .HeaderText = "ชื่อสินค้า"
    '            .Width = 300
    '            .DataPropertyName = "itemnumber"
    '        End With

    '        With Col_OrderQty
    '            .HeaderText = "จำนวน"
    '            .Width = 60
    '            .DefaultCellStyle.Format = "NO"
    '            .DefaultCellStyle.NullValue = "1"
    '            .DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
    '            .DataPropertyName = ""
    '        End With

    '        With Col_Uom
    '            .HeaderText = "หน่วยบรรจุ"
    '            .Width = 100
    '            .DataPropertyName = "containunit"
    '        End With

    '        With Col_OnHandQty
    '            .HeaderText = "ในสต็อก"
    '            .Width = 80
    '            .DefaultCellStyle.Format = "N0"
    '            .DefaultCellStyle.NullValue = "0"
    '            .DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
    '            .DataPropertyName = "onhandqty"
    '            .ReadOnly = True
    '        End With

    '        With Col_Price
    '            .HeaderText = "ราคากลาง"
    '            .Width = 80
    '            .DefaultCellStyle.Format = "N0"
    '            .DefaultCellStyle.NullValue = "0"
    '            .DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
    '            .DataPropertyName = "priceperitem"
    '            .ReadOnly = True
    '        End With


    '        With Col_Totalprice
    '            .HeaderText = "รวมมูลค่า"
    '            .Width = 80
    '            .DefaultCellStyle.Format = "N0"
    '            .DefaultCellStyle.NullValue = "0"
    '            .DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
    '            .DataPropertyName = "totalprice"
    '            .ReadOnly = True
    '        End With


    '        With Col_Note
    '            .HeaderText = "หมายเหตุ"
    '            .Width = 100
    '            .DataPropertyName = "note"
    '        End With



    '        With GridOrderdetail

    '            .AutoGenerateColumns = False
    '            .Columns.Add(Col_Products)
    '            .Columns.Add(Col_OrderQty)
    '            .Columns.Add(Col_Uom)
    '            .Columns.Add(Col_OnHandQty)
    '            '.Columns.Add(Col_Drawqty)
    '            .Columns.Add(Col_Price)
    '            .Columns.Add(Col_Totalprice)
    '            .Columns.Add(Col_Note)
    '            .Columns.Add(Col_OrderNumber)

    '            Select Case NewEntry_State
    '                Case True

    '                Case False
    '                    Dim DTPROrder As New DataTable
    '                    Strsql = "Select ordernumber,orderdate,yearbudget,site,department,orderedby,producttype,reason from prorder where ordernumber = " & Me.TxtOrderNumber.Text
    '                    Me.EventLog1.WriteEntry(Me.Name & " : " & Strsql, EventLogEntryType.Information, 1)
    '                    DTPROrder = Mysql.GetMYSQLDataTable(Strsql, "prorder")
    '                    ' Me.TxtOrderNumber.Text = DTPROrder.Rows(0).Item("ordernumber").ToString
    '                    Me.TxtOrderedBy.Text = DTPROrder.Rows(0).Item("orderedby").ToString
    '                    Me.TxtProductType.Text = DTPROrder.Rows(0).Item("producttype").ToString
    '                    Me.TxtReason.Text = DTPROrder.Rows(0).Item("reason").ToString
    '                    Me.DTPOrderedDate.Value = DTPROrder.Rows(0).Item("orderdate")
    '                    Me.CboSite.SelectedValue = DTPROrder.Rows(0).Item("site")
    '                    Me.cboDepartment.SelectedValue = DTPROrder.Rows(0).Item("department")
    '                    Me.CboYearBudget.SelectedValue = DTPROrder.Rows(0).Item("yearbudget")


    '                    Strsql = "Select * from prdetail where ordernumber = '" & Me.TxtOrderNumber.Text & "'"
    '                    Me.EventLog1.WriteEntry(Me.Name & " : " & Strsql, EventLogEntryType.Information, 1)
    '                    DA = New MySqlDataAdapter(Strsql, ClsSQLhelper.MySQLConnectionStr)
    '                    DA.Fill(DSWHIS, "prdetail")
    '                    '  BDS = New BindingSource(DSWHIS.Tables("prdetail"), "prdetail")
    '                    CMDB = New MySqlCommandBuilder(DA)
    '                    .DataSource = DSWHIS.Tables("prdetail")
    '            End Select


    '        End With



    '        'Select Case TxtPONumber.Text
    '        '    Case Is <> ""

    '        '        Try
    '        '            Strsql = "Select * from poorder where ordernumber = " & Me.TxtPONumber.Text
    '        '            Me.EventLog1.WriteEntry(Me.Name & " : " & Strsql, EventLogEntryType.Information, 1)
    '        '            DS_PROrder = Mysql.GetMYSQLDataset(Strsql, "DSprorder", "prorder")
    '        '            Me.cboDepartment.SelectedValue = DS_PROrder.Tables(0).Rows(0).Item("department")
    '        '            ' Me.TxtReason.Text = DS_PROrder.Tables(0).Rows(0).Item("reason").ToString
    '        '            'Me.TxtProductType.Text = DS_PROrder.Tables(0).Rows(0).Item("producttype").ToString
    '        '            Me.DTPOrderedDate.Value = DS_PROrder.Tables(0).Rows(0).Item("orderdate").ToString
    '        '            'Me.TxtOrderedBy.Text = DS_PROrder.Tables(0).Rows(0).Item("orderedby").ToString
    '        '            '   frmPROrder.CboOrderStatus.SelectedValue = DS_PROrder.Tables(0).Rows(0).Item("orderstatus").ToString
    '        '            frmPROrder.Cbo_site.SelectedValue = DS_PROrder.Tables(0).Rows(0).Item("site").ToString
    '        '            DS_PROrder.Dispose()

    '        '            Strsql = "Select itemcode,orderedqty,price from podetail where ordernumber = " & Me.TxtPONumber.Text & " order by itemrownumber"
    '        '            Me.EventLog1.WriteEntry(Me.Name & " : " & Strsql, EventLogEntryType.Information, 1)
    '        '            DT_PRdetail = Mysql.GetMYSQLDataTable(Strsql, "prdetail")
    '        '            If DT_PRdetail.Rows.Count <> 0 Then
    '        '                For i = 0 To DT_PRdetail.Rows.Count - 1
    '        '                    .Rows.Add(DT_PRdetail.Rows(i).Item(0).ToString, DT_PRdetail.Rows(i).Item(1).ToString, DT_PRdetail.Rows(i).Item(2).ToString, Val(DT_PRdetail.Rows(i).Item(1).ToString) * Val(DT_PRdetail.Rows(i).Item(2).ToString))
    '        '                Next
    '        '            End If
    '        '        Catch ex As Exception
    '        '            MsgBox(ex.Message)
    '        '        End Try

    '        '    Case ""

    '        'End Select
    '    End With
    'End Sub
#End Region

   

    'Function Load_ComboboxData_Vendor(Optional ByVal StrCondition As String = "") As Boolean
    '    Try
    '        Dim DS As New DataSet

    '        Load_ComboboxData_Vendor = False
    '        Strsql = "select * from vendor where active = 1 " & StrCondition & "  and siteid = " & My.Settings.CurrentSite & "  order by vendorname "
    '        Me.EventLog1.WriteEntry(Me.Name & " : " & Strsql, EventLogEntryType.Information, 1)
    '        DS = Mysql.GetMYSQLDataset(Strsql, "DS_vendor", "vendor")
    '        ' Me.EventLog1.WriteEntry(Strsql, EventLogEntryType.Information)
    '        With Me.CboVendor
    '            Me.CboVendor.Text = ""
    '            ' If DS.Tables(0).Rows.Count = 0 Then Exit Function
    '            .DataSource = DS.Tables(0)
    '            .DisplayMember = "vendorname"
    '            .ValueMember = "vendorid"
    '            .SelectionStart = 0
    '        End With
    '        Load_ComboboxData_Vendor = True
    '    Catch ex As Exception
    '        Me.EventLog1.WriteEntry(Me.Name & " : Function Load_ComboboxData_band - " & ex.Message, EventLogEntryType.Error, 1)
    '        MsgBox(ex.Message)
    '    End Try
    'End Function
    'Sub Load_CboProrderlist()
    '    Dim DS_Prorder As New DataTable
    '    Strsql = "select * from prorder order by ordernumber desc"
    '    DS_Prorder = Mysql.GetMYSQLDataTable(Strsql, "prorder")
    '    With Me.CboPROrder
    '        .DataSource = DS_Prorder
    '        .DisplayMember = "ordernumber"
    '        .ValueMember = "ordernumber"
    '        .SelectionStart = 0
    '    End With
    'End Sub
    'Sub Load_CboOrderStatus()
    '    Dim DT_OrderStatus As New DataSet
    '    Strsql = "Select * from orderstatus where active = 1"
    '    DT_OrderStatus = Mysql.GetMYSQLDataset(Strsql, "DSSite", "Site")
    '    With CboOrderStatus
    '        .DataSource = DT_OrderStatus.Tables(0)
    '        .DisplayMember = "orderedtatus"
    '        .ValueMember = "orderstatusid"
    '        .SelectionStart = 0
    '    End With
    '    DT_OrderStatus.Dispose()

    'End Sub

    Private Sub FrmPoDetail_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        EventLog1.Source = "WHIS"
        EventLog1.Log = "WHIS"
        Load_Budget()
        Load_Site()
        Load_CboDepartment()
        Load_CboOrderStatus()

        Load_Data()
        'frmPoOrder.Hide()
        ' Load_ComboboxData_Vendor()
        ' Load_CboDepartment()
        'Load_CboProrderlist()
        'cls_whisframework.Load_CboOrderStatus(Me.CboOrderStatus)

        '' frmmain.mSpk.Play("process")
        'Select Case Me.TxtPONumber.Text
        '    Case ""

        '    Case Is <> ""
        '        Dim DTPO As New DataTable
        '        Strsql = "Select * from poorder where ordernumber = " & Me.TxtPONumber.Text
        '        Me.EventLog1.WriteEntry(Me.Name & " : " & Strsql, EventLogEntryType.Information, 1)
        '        DTPO = Mysql.GetMYSQLDataTable(Strsql, "Prorder")



        '        DTPO.Dispose()

        'End Select

        ' On_Load = False

    End Sub
  
    Private Sub BtnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnCancel.Click
        'Strsql = "Update poorder set Lockedby = '',lockeddatetime = null,lockedbycomputer = '' where ordernumber = " & Me.TxtPONumber.Text
        'Me.EventLog1.WriteEntry(Me.Name & " : " & Strsql, EventLogEntryType.Information, 1)
        'Mysql.MySQLExecute(Strsql)
        Me.Dispose()
        'frmPoOrder.WindowState = FormWindowState.Maximized
        'frmPoOrder.Show()

    End Sub
    Private Sub BtnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnSave.Click
        Try
            If MsgBox("คุณต้องการบันทึกข้อมูลใช่หรือไม่", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "ยืนยัน") = MsgBoxResult.No Then Exit Sub
            If Validate_Data() = False Then Exit Sub
            Select Case Me.NewEntry_State
                Case True
                    Strsql = "Insert into poorder (site,yearbudget,ordernumber,orderdate,createdby,createddate,orderstatus,prorder,department,vat,discount,totalprice,grandprice) "
                    Strsql = Strsql & " values (" & Me.CboSite.SelectedValue & ",'" & Me.CboYearBudget.SelectedValue & "','" & Me.TxtOrderNumber.Text & "' ,'"
                    Strsql = Strsql & Pn_Framework.MysqlDateTimeFormat(Me.DTPOrderedDate.Value, False) & "','" & My.Settings.Currentuser & "','" & Pn_Framework.MysqlDateTimeFormat(Now, False)
                    Strsql = Strsql & "'," & Me.CboOrderStatus.SelectedValue & ",'" & Me.TxtPROrder.Text & "'," & Me.cboDepartment.SelectedValue & "," & Me.NUDVat.Value & "," & Me.NUDDiscount.Value
                    Strsql = Strsql & "," & Replace(Me.TxtTotalPrice.Text, ",", "") & "," & Replace(Me.TxtGrandPrice.Text, ",", "") & " )"
                    Mysql.MySQLExecute(Strsql)

                    With Me.GridOrderdetail
                        For i As Integer = 0 To .RowCount - 2

                            Strsql = "Insert into podetail (itemrownumber,itemnumber,orderedqty,containunit,onhandqty,priceperitem,currprice,totalprice,note,ordernumber)"
                            Strsql = Strsql & " Values (" & i & "," & .Rows(i).Cells(0).Value & "," & IIf(.Rows(i).Cells(1).Value = "", 1, .Rows(i).Cells(1).Value) & ",'" & .Rows(i).Cells(2).Value & "'"
                            Strsql = Strsql & "," & .Rows(i).Cells(3).Value
                            Strsql = Strsql & "," & .Rows(i).Cells(4).Value
                            Strsql = Strsql & "," & .Rows(i).Cells(5).Value
                            Strsql = Strsql & "," & .Rows(i).Cells(6).Value
                            Strsql = Strsql & ",'" & .Rows(i).Cells(7).Value & "'"
                            Strsql = Strsql & ",'" & .Rows(i).Cells(8).Value & "')"
                            Mysql.MySQLExecute(Strsql)
                        Next
                    End With

                Case False
                    Strsql = "Update poorder set  site =  " & Me.CboSite.SelectedValue & " , department = " & Me.cboDepartment.SelectedValue & " , orderdate = '" & Pn_Framework.MysqlDateTimeFormat(Me.DTPOrderedDate.Value) & "', orderedby = '" & Me.TxtOrderedBy.Text & "' ,Updatedby = '" & My.Settings.Currentuser & "' , updateddatetime = '" & Pn_Framework.MysqlDateTimeFormat(Me.DTPOrderedDate.Value) & "',producttype = '" & Me.TxtProductType.Text & "',reason = '" & Me.TxtReason.Text & "'"
                    Strsql = Strsql & " where ordernumber =  " & Me.TxtOrderNumber.Text
                    Me.EventLog1.WriteEntry(Me.Name & " : " & Strsql, EventLogEntryType.Information, 1)
                    Mysql.MySQLExecute(Strsql)

                    DA.Update(DSWHIS, "podetail")

                    'If Me.CboOrderStatus.SelectedValue = 3 Then
                    '    Strsql = "update poorder set orderstatus = 3,canceltype = " & DlgCancelPROrder.CboCancelType.SelectedValue & ",cancelnote = '" & DlgCancelPROrder.TxtCancelNote.Text & "',canceldatetime = '" & Pn_Framework.MysqlDateTimeFormat(DlgCancelPROrder.DTPCancelDate.Value, True) & "',cancelby = '" & DlgCancelPROrder.TxtCanceluser.Text & "' where ordernumber = '" & DlgCancelPROrder.TxtPrOrder.Text & "'"
                    '    Mysql.MySQLExecute(Strsql)
                    'End If


                    MsgBox("ปรับปรุงข้อมูลเสร็จเรียบร้อยแล้ว")
            End Select

          
            '        Strsql = "Select ordernumber from poorder order by ordernumber desc"
            '        Me.TxtPONumber.Text = Mysql.MySQLExecuteScalar(Strsql)


            '    Else
            '        Strsql = "Update poorder set site= " & frmPoOrder.Cbo_site.SelectedValue & ",vendor = " & Me.CboVendor.SelectedValue & ","
            '        Strsql = Strsql & " orderdate = '" & Pn_Framework.MysqlDateTimeFormat(Me.DTPOrderedDate.Value, False) & "'"
            '        Strsql = Strsql & " ,duedate = '" & Pn_Framework.MysqlDateTimeFormat(Me.DtpDueDate.Value, False) & "'"
            '        Strsql = Strsql & " ,updatedby = '" & My.Settings.Currentuser & "',orderstatus = " & Me.CboOrderStatus.SelectedValue
            '        Strsql = Strsql & ",prorder = " & Me.CboPROrder.SelectedValue & ",department = " & Me.cboDepartment.SelectedValue & ",updateddatetime='" & Pn_Framework.MysqlDateTimeFormat(Now, True) & "'"
            '        Mysql.MySQLExecute(Strsql)


            '    End If

            '    With GridOrderdetail
            '        Dim iRow As New Integer
            '        If Me.TxtPONumber.Text = "" Then
            '            For iRow = 0 To Me.GridOrderdetail.RowCount - 2
            '                Strsql = "Insert into podetail (itemrownumber,ordernumber,itemcode,orderedqty,price) "
            '                Strsql = Strsql & " values( " & iRow + 1 & "," & Me.TxtPONumber.Text & ",'" & .Rows(iRow).Cells(0).Value & "'," & If(.Rows(iRow).Cells(1).Value Is Nothing, 1, .Rows(iRow).Cells(1).Value) & "," & If(.Rows(iRow).Cells(2).Value Is Nothing, 1, .Rows(iRow).Cells(2).Value)
            '                Strsql = Strsql & ")"
            '                Mysql.MySQLExecute(Strsql)
            '            Next
            '        Else

            '            Dim CurrDBOrderRowsNo As Integer
            '            Dim CurrGridRow As Integer = Me.GridOrderdetail.Rows.Count - 2

            '            Strsql = "select count(*) from podetail where ordernumber = " & Me.TxtPONumber.Text
            '            CurrDBOrderRowsNo = Mysql.MySQLExecuteScalar(Strsql)

            '            Select Case CurrDBOrderRowsNo
            '                Case 0
            '                    For iRow = 0 To Me.GridOrderdetail.RowCount - 2
            '                        Strsql = "Insert into podetail (itemrownumber,ordernumber,itemcode,orderedqty,price) "
            '                        Strsql = Strsql & " values( " & iRow + 1 & "," & Me.TxtPONumber.Text & ",'" & .Rows(iRow).Cells(0).Value & "'," & If(.Rows(iRow).Cells(1).Value Is Nothing, 1, .Rows(iRow).Cells(1).Value) & "," & If(.Rows(iRow).Cells(2).Value Is Nothing, 1, .Rows(iRow).Cells(2).Value)
            '                        Strsql = Strsql & ")"
            '                        Mysql.MySQLExecute(Strsql)
            '                    Next
            '                Case Is > 0
            '                    'Check New Entry on Grid
            '                    'Incase of current total row < total rows in podetail in database
            '                    If ((Me.GridOrderdetail.Rows.Count - 1) < CurrDBOrderRowsNo) Then
            '                        Strsql = "Delete from podetail where ordernumber = " & Me.TxtPONumber.Text & " and itemrownumber > " & Me.GridOrderdetail.Rows.Count - 1
            '                        Mysql.MySQLExecute(Strsql)
            '                        For iRow = 0 To Me.GridOrderdetail.Rows.Count - 2
            '                            Strsql = "Update podetail set itemcode = '" & .Rows(iRow).Cells(0).Value & "',orderedqty = " & If(.Rows(iRow).Cells(1).Value Is Nothing, 1, .Rows(iRow).Cells(1).Value) & ",price = " & If(.Rows(iRow).Cells(2).Value Is Nothing, 1, .Rows(iRow).Cells(2).Value) & " where itemrownumber = " & iRow + 1
            '                            Mysql.MySQLExecute(Strsql)
            '                        Next
            '                    ElseIf ((Me.GridOrderdetail.Rows.Count - 1) = CurrDBOrderRowsNo) Then
            '                        'No New Row entry update all rows
            '                        For iRow = 0 To Me.GridOrderdetail.Rows.Count - 2
            '                            Strsql = "Update podetail set itemcode = '" & .Rows(iRow).Cells(0).Value & "',orderedqty = " & If(.Rows(iRow).Cells(1).Value Is Nothing, 1, .Rows(iRow).Cells(1).Value) & ",price = " & If(.Rows(iRow).Cells(2).Value Is Nothing, 1, .Rows(iRow).Cells(2).Value) & " where itemrownumber = " & iRow + 1
            '                            Mysql.MySQLExecute(Strsql)
            '                        Next
            '                    Else
            '                        'Found new Row Entry
            '                        For iRow = (CurrDBOrderRowsNo) To (Me.GridOrderdetail.Rows.Count - 2)
            '                            Strsql = "Insert into podetail (itemrownumber,ordernumber,itemcode,orderedqty,price) "
            '                            Strsql = Strsql & " values( " & iRow + 1 & "," & Me.TxtPONumber.Text & ",'" & .Rows(iRow).Cells(0).Value & "'," & If(.Rows(iRow).Cells(1).Value Is Nothing, 1, .Rows(iRow).Cells(1).Value) & "," & If(.Rows(iRow).Cells(2).Value Is Nothing, 1, .Rows(iRow).Cells(2).Value)
            '                            Strsql = Strsql & ")"
            '                            Mysql.MySQLExecute(Strsql)
            '                        Next
            '                    End If

            '            End Select

            '        End If
            '    End With
            MsgBox("บันทึกสำเร็จแล้ว", MsgBoxStyle.Information)
        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "BtnSave_Click"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try
    End Sub

    Private Sub GridOrderdetail_CellBeginEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellCancelEventArgs) Handles GridOrderdetail.CellBeginEdit
        Try
            If Me.TxtOrderNumber.Text = "" Then
                MsgBox("กรุณาใส่เลขที่ใบสั่งซื้อ", MsgBoxStyle.Exclamation)
                e.Cancel = True
                Me.TxtOrderNumber.Focus()
                Exit Sub
            End If
            If Me.NewEntry_State = False Then Exit Sub
            Strsql = "Select Count(*) as rowcount from poorder where ordernumber = '" & Me.TxtOrderNumber.Text & "' and site = " & Me.CboSite.SelectedValue
            If Mysql.MySQLExecuteScalar(Strsql) > 0 Then
                MsgBox("Order นี้ได้ถูกใช้งานแล้ว ไม่สามารถใช้งานได้อีก", MsgBoxStyle.Critical)
                e.Cancel = True
                Exit Sub
            End If
        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "GridOrderdetail_CellBeginEdit"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try


        'If Me.GridOrderdetail.Rows.Count = 0 Then Exit Sub
        'If On_Load = True Then Exit Sub
        'If IsNumeric(Me.GridOrderdetail.Rows(GridOrderdetail.CurrentRow.Index).Cells(1).Value) = False Or IsNumeric(Me.GridOrderdetail.Rows(GridOrderdetail.CurrentRow.Index).Cells(2).Value) = False Then
        ' Exit Sub
        ' End If
        'Exit Sub
        '  Me.lOldValue = GridOrderdetail.CurrentCell.Value

    End Sub
    Private Sub GridOrderdetail_CellEndEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles GridOrderdetail.CellEndEdit
        Try
            If Me.TxtPROrder.Text = "" Then Me.BtnimportPR.Enabled = False

            If e.ColumnIndex <> 1 And e.ColumnIndex <> 5 Then Exit Sub
            With GridOrderdetail
                Try
                    ' .Rows(.CurrentCell.RowIndex).Cells(4).Value = CType(.Rows(.CurrentCell.RowIndex).Cells(3).Value, Integer) - CType(.Rows(.CurrentCell.RowIndex).Cells(1).Value, Integer)
                    .Rows(.CurrentCell.RowIndex).Cells(6).Value = IIf(CType(.Rows(.CurrentCell.RowIndex).Cells(1).Value, Integer) = Nothing, 1, CType(.Rows(.CurrentCell.RowIndex).Cells(1).Value, Integer)) * CType(.Rows(.CurrentCell.RowIndex).Cells(5).Value, Integer)
                    Dim Totalprice As Integer
                    For i As Integer = 0 To GridOrderdetail.RowCount - 1
                        Totalprice = Totalprice + CType(.Rows(.CurrentCell.RowIndex).Cells(6).Value, Integer)
                    Next
                    Me.TxtTotalPrice.Text = String.Format("{0:n2}", Totalprice)
                Catch ex As Exception
                    MsgBox(Err.Number & " : " & ex.Message)
                End Try


            End With
        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "GridOrderdetail_CellEndEdit"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try

        'If Me.GridOrderdetail.Rows.Count = 0 Then Exit Sub
        '  If On_Load = True Then Exit Sub
        ' Me.lNewValue = GridOrderdetail.CurrentCell.Value

        'Me.GridOrderdetail.Rows(e.RowIndex).Cells(3).Value = Val(If(Me.GridOrderdetail.Rows(e.RowIndex).Cells(1).Value Is Nothing, 1, Me.GridOrderdetail.Rows(e.RowIndex).Cells(1).Value)) * Val(Me.GridOrderdetail.Rows(e.RowIndex).Cells(2).Value)


    End Sub

    Private Sub GridOrderdetail_CellValidating(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellValidatingEventArgs) Handles GridOrderdetail.CellValidating
        'If IsNumeric(Me.GridOrderdetail.Rows(GridOrderdetail.CurrentRow.Index).Cells(1).Value) = False Or IsNumeric(Me.GridOrderdetail.Rows(GridOrderdetail.CurrentRow.Index).Cells(2).Value) = False Then
        '    e.Cancel = True
        'End If

    End Sub

    Private Sub GridOrderdetail_CellValueChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles GridOrderdetail.CellValueChanged
      
        'If GridOrderdetail.Rows(e.RowIndex).Cells(0).Value = Nothing Then
        '    MsgBox("กรุณาเลือกรายการก่อน", MsgBoxStyle.Exclamation)
        '    If e.RowIndex <> GridOrderdetail.NewRowIndex Then
        '        GridOrderdetail.AllowUserToAddRows = False
        '    End If

        '    Exit Sub
        'Else
        '    GridOrderdetail.AllowUserToAddRows = True
        'End If

        'If GridOrderdetail.Rows(e.RowIndex).Cells(1).Value = Nothing Then
        '    MsgBox("กรุณาใส่จำนวน", MsgBoxStyle.Exclamation)
        '    GridOrderdetail.AllowUserToAddRows = False
        '    Exit Sub
        'Else
        '    GridOrderdetail.AllowUserToAddRows = True
        'End If

        'If GridOrderdetail.Rows(e.RowIndex).Cells(2).Value = Nothing Then
        '    MsgBox("กรุณาใส่ราคา", MsgBoxStyle.Exclamation)
        '    GridOrderdetail.AllowUserToAddRows = False
        '    Exit Sub
        'Else
        '    GridOrderdetail.AllowUserToAddRows = True
        'End If

        'If e.ColumnIndex = 1 Then
        '    If IsNumeric(GridOrderdetail.Rows(e.RowIndex).Cells(1).Value) = False Then
        '        MsgBox("ระบุเป็นตัวเลขเท่านั้น", MsgBoxStyle.Exclamation)
        '        'GridOrderdetail.Rows(e.RowIndex).Cells(1).Value = lOldValue
        '    End If
        'End If
        'If e.ColumnIndex = 2 Then
        '    If IsNumeric(GridOrderdetail.Rows(e.RowIndex).Cells(2).Value) = False Then
        '        ' GridOrderdetail.Rows(e.RowIndex).Cells(2).Value = lOldValue
        '        MsgBox("ระบุเป็นตัวเลขเท่านั้น", MsgBoxStyle.Exclamation)
        '    End If
        'End If
    End Sub

   

    Private Sub BtnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnDelete.Click
        'If (MsgBox("คุณต้องการลบ Order " & Me.TxtPONumber.Text & " นี้ใช่หรือไม่ ?", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "ยืนยันการลบ") = MsgBoxResult.No) Then Exit Sub
        'Strsql = "Delete from podetail where ordernumber = " & Me.TxtPONumber.Text
        'Mysql.MySQLExecute(Strsql)

        'Strsql = "Delete from poorder where ordernumber = " & Me.TxtPONumber.Text
        'Mysql.MySQLExecute(Strsql)
        'MsgBox("ลบข้อมูลเรียบร้อยแล้ว", MsgBoxStyle.Information)
        'Me.Dispose()
    End Sub



    Private Sub CboPR_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    Private Sub GridOrderdetail_DefaultValuesNeeded(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowEventArgs) Handles GridOrderdetail.DefaultValuesNeeded
        Try
            With Me.GridOrderdetail
                .Rows(e.Row.Index).Cells(8).Value = Me.TxtOrderNumber.Text
            End With
        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "GridOrderdetail_DefaultValuesNeeded"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try

    End Sub

    Private Sub GridOrderdetail_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles GridOrderdetail.CellClick
        Try
            With Me.GridOrderdetail
                .Rows(.CurrentCell.RowIndex).Cells(8).Value = Me.TxtOrderNumber.Text

            End With
        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "GridOrderdetail_CellClick"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try

    End Sub

    Private Sub GridOrderdetail_CurrentCellDirtyStateChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridOrderdetail.CurrentCellDirtyStateChanged
        Try
            With GridOrderdetail
                If .IsCurrentCellDirty = False Then Return

                .CommitEdit(DataGridViewDataErrorContexts.Commit)

                If .CurrentCell.ColumnIndex = 0 Then

                    Dim colUnit As New DataGridViewComboBoxCell
                    Strsql = "select  containunit  from itemcontainunit  where itemcontainunit.active = 1 and itemid = " & .Rows(.CurrentCell.RowIndex).Cells(0).Value & " order by  containunit "
                    Dim DTUnit As New DataTable
                    DTUnit = Mysql.GetMYSQLDataTable(Strsql, "itemcontainunit")
                    colUnit.Items.Clear()
                    For i As Integer = 0 To DTUnit.Rows.Count - 1
                        colUnit.Items.Add(DTUnit.Rows(i).Item(0).ToString)
                    Next
                    .Rows(.CurrentCell.RowIndex).Cells(2) = colUnit
                    .Rows(.CurrentCell.RowIndex).Cells(2).Value = DTUnit.Rows(0).Item(0).ToString
                    Dim DTITEM As New DataTable
                    Strsql = "Select onhandqty from inventory where itemnumber = " & GridOrderdetail.Rows(.CurrentCell.RowIndex).Cells(0).Value & " and site = " & Me.CboSite.SelectedValue
                    If Mysql.MySQLExecuteScalar(Strsql) = Nothing Then
                        .Rows(.CurrentCell.RowIndex).Cells(3).Value = 0
                    Else
                        .Rows(.CurrentCell.RowIndex).Cells(3).Value = Mysql.MySQLExecuteScalar(Strsql)
                    End If
                    Dim intNeedMore As Integer = CType(.Rows(.CurrentCell.RowIndex).Cells(3).Value, Integer) - CType(.Rows(.CurrentCell.RowIndex).Cells(1).Value, Integer)
                    'If intNeedMore >= 0 Then
                    '    .Rows(.CurrentCell.RowIndex).Cells(4).Style.ForeColor = Color.Black
                    '    .Rows(.CurrentCell.RowIndex).Cells(4).Value = 0
                    'Else
                    '    .Rows(.CurrentCell.RowIndex).Cells(4).Value = Math.Abs(intNeedMore)
                    '    .Rows(.CurrentCell.RowIndex).Cells(4).Style.ForeColor = Color.Red
                    'End If

                    Strsql = "Select standdardprice  from item where itemnumber = " & GridOrderdetail.Rows(.CurrentCell.RowIndex).Cells(0).Value & " "
                    .Rows(.CurrentCell.RowIndex).Cells(4).Value = Mysql.MySQLExecuteScalar(Strsql)
                    .Rows(.CurrentCell.RowIndex).Cells(4).Value = CType(.Rows(.CurrentCell.RowIndex).Cells(1).Value, Integer) * CType(.Rows(.CurrentCell.RowIndex).Cells(4).Value, Integer)
                    'DTITEM = Mysql.GetMYSQLDa8taTable(Strsql, "itemcontainunit")
                Else

                End If
            End With
        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "GridOrderdetail_CurrentCellDirtyStateChanged"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try
    End Sub

    Private Sub BtnimportPR_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnimportPR.Click
        Try
            If Me.TxtOrderNumber.Text = "" Then
                MsgBox("กรุณาใส่หมายเลขใบ PO", MsgBoxStyle.Exclamation)
                Me.TxtOrderNumber.Focus()
                Exit Sub
            End If

            Strsql = "Select ordernumber from poorder where ordernumber = '" & Me.TxtOrderNumber.Text & "' and site = " & Me.CboSite.SelectedValue
            If Mysql.MySQLExecuteScalar(Strsql) > 0 Then
                MsgBox("Order นี้ได้ถูกใช้งานแล้ว ไม่สามารถใช้งานได้อีก", MsgBoxStyle.Critical)
                Exit Sub
            End If

            If frmprorderlist.ShowDialog = Windows.Forms.DialogResult.Cancel Then Exit Sub
            Me.TxtPROrder.Text = frmprorderlist.SelectedOrder



            Strsql = "Select * from podetail where ordernumber = '" & Me.TxtOrderNumber.Text & "'"
            Me.EventLog1.WriteEntry(Me.Name & " : " & Strsql, EventLogEntryType.Information, 1)
            DA = New MySqlDataAdapter(Strsql, ClsSQLhelper.MySQLConnectionStr)
            DA.Fill(DSWHIS, "podetail")
            '  BDS = New BindingSource(DSWHIS.Tables("prdetail"), "prdetail")
            CMDB = New MySqlCommandBuilder(DA)
            Me.GridOrderdetail.DataSource = DSWHIS.Tables("podetail")


            Dim DTPROrder As New DataTable
            Strsql = "Select ordernumber,orderdate,orderstatus,yearbudget,site,department,orderedby,producttype,reason from prorder where ordernumber = " & Me.TxtPROrder.Text
            Me.EventLog1.WriteEntry(Me.Name & " : " & Strsql, EventLogEntryType.Information, 1)
            DTPROrder = Mysql.GetMYSQLDataTable(Strsql, "prorder")

            ' Me.TxtOrderNumber.Text = DTPROrder.Rows(0).Item("ordernumber").ToString
            Me.TxtOrderedBy.Text = DTPROrder.Rows(0).Item("orderedby").ToString
            Me.TxtOrderedBy.ReadOnly = True
            Me.TxtProductType.Text = DTPROrder.Rows(0).Item("producttype").ToString
            Me.TxtProductType.ReadOnly = True
            Me.TxtReason.Text = DTPROrder.Rows(0).Item("reason").ToString
            Me.TxtReason.ReadOnly = True
            Me.DTPOrderedDate.Value = DTPROrder.Rows(0).Item("orderdate")
            Me.DTPOrderedDate.Enabled = False
            Me.CboSite.SelectedValue = DTPROrder.Rows(0).Item("site")
            Me.CboSite.Enabled = False
            Me.cboDepartment.SelectedValue = DTPROrder.Rows(0).Item("department")
            Me.cboDepartment.Enabled = False
            Me.CboYearBudget.SelectedValue = DTPROrder.Rows(0).Item("yearbudget")
            Me.CboYearBudget.Enabled = False
            Me.CboOrderStatus.SelectedValue = DTPROrder.Rows(0).Item("orderstatus")
            Me.CboOrderStatus.Enabled = False

            Strsql = "select itemnumber,orderedqty,containunit,onhandqty,priceperitem,totalprice,note,ordernumber from prdetail where ordernumber = '" & Me.TxtPROrder.Text & "' order by itemrownumber"
            Dim DTPROrderDetail As New DataTable
            DTPROrderDetail = Mysql.GetMYSQLDataTable(Strsql, "prdetail")
            Dim DRpodetail As DataRow
            Dim TotoalPrice As Integer

            For i As Integer = 0 To DTPROrderDetail.Rows.Count - 1
                DRpodetail = DSWHIS.Tables("podetail").NewRow
                DRpodetail("itemnumber") = DTPROrderDetail.Rows(i).Item("itemnumber")
                DRpodetail("orderedqty") = DTPROrderDetail.Rows(i).Item("orderedqty")
                DRpodetail("containunit") = DTPROrderDetail.Rows(i).Item("containunit")
                DRpodetail("onhandqty") = DTPROrderDetail.Rows(i).Item("onhandqty")
                DRpodetail("priceperitem") = DTPROrderDetail.Rows(i).Item("priceperitem")
                DRpodetail("totalprice") = DTPROrderDetail.Rows(i).Item("totalprice")
                DRpodetail("note") = DTPROrderDetail.Rows(i).Item("note")
                DRpodetail("ordernumber") = Me.TxtOrderNumber.Text
                DSWHIS.Tables("podetail").Rows.Add(DRpodetail)
                TotoalPrice = TotoalPrice + CType(DTPROrderDetail.Rows(i).Item("priceperitem"), Integer)

            Next
            Me.TxtTotalPrice.Text = String.Format("{0:n2}", TotoalPrice)
            Me.TxtGrandPrice.Text = String.Format("{0:n2}", CType(Me.TxtTotalPrice.Text, Integer) - (CType(Me.txtVat.Text, Integer) + CType(Me.TxtDiscount.Text, Integer)))

            Me.TxtOrderNumber.ReadOnly = True
            Me.GridOrderdetail.AllowUserToAddRows = False
            Me.BtnimportPR.Enabled = False
            With Me.GridOrderdetail
                .Columns(0).ReadOnly = True
                .Columns(1).ReadOnly = True
                .Columns(2).ReadOnly = True
                .Columns(5).DefaultCellStyle.BackColor = Color.YellowGreen

            End With
        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "BtnimportPR_Click"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try

    End Sub



    Private Sub TxtTotalPrice_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TxtTotalPrice.TextChanged
        Try
            Me.TxtDiscount.Text = String.Format("{0:n2}", (Me.TxtTotalPrice.Text * NUDDiscount.Value) / 100)
            Me.txtVat.Text = String.Format("{0:n2}", (Me.TxtTotalPrice.Text * Me.NUDVat.Value) / 100)
            Me.TxtGrandPrice.Text = String.Format("{0:n2}", CType(Me.TxtTotalPrice.Text, Integer) - (CType(Me.txtVat.Text, Integer) + CType(Me.TxtDiscount.Text, Integer)))
        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "TxtTotalPrice_TextChanged"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try

    End Sub

    Private Sub NUDDiscount_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NUDDiscount.ValueChanged
        Try
            Me.TxtDiscount.Text = String.Format("{0:n2}", (Me.TxtTotalPrice.Text * NUDDiscount.Value) / 100)
            Me.TxtGrandPrice.Text = String.Format("{0:n2}", CType(Me.TxtTotalPrice.Text, Integer) - (CType(Me.txtVat.Text, Integer) + CType(Me.TxtDiscount.Text, Integer)))
        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "NUDDiscount_ValueChanged"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try
      

    End Sub

    Private Sub NUDVat_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles NUDVat.Click
        Try
            If Me.NUDVat.Value = 0 Then Me.NUDVat.Value = 7
        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "NUDVat_Click"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try


    End Sub

    Private Sub NUDVat_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NUDVat.ValueChanged
        Try
            Me.txtVat.Text = String.Format("{0:n2}", (Me.TxtTotalPrice.Text * Me.NUDVat.Value) / 100)
            Me.TxtGrandPrice.Text = String.Format("{0:n2}", CType(Me.TxtTotalPrice.Text, Integer) - (CType(Me.txtVat.Text, Integer) + CType(Me.TxtDiscount.Text, Integer)))
        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "NUDVat_ValueChanged"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try
       

    End Sub

    Private Sub txtVat_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtVat.TextChanged
        'Me.TxtGrandPrice.Text = CType(Me.TxtTotalPrice.Text, Integer) - (CType(Me.txtVat.Text, Integer) + CType(Me.TxtDiscount.Text, Integer))
    End Sub

    Private Sub TxtDiscount_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TxtDiscount.TextChanged
        'Me.TxtGrandPrice.Text = CType(Me.TxtTotalPrice.Text, Integer) - (CType(Me.txtVat.Text, Integer) + CType(Me.TxtDiscount.Text, Integer))
    End Sub

    Private Sub GridOrderdetail_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles GridOrderdetail.CellContentClick

    End Sub

    Private Sub TxtOrderNumber_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TxtOrderNumber.TextChanged

    End Sub

    Private Sub Panel4_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Panel4.Paint

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    End Sub
End Class